package com.panda.springcloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.springcloud.clients.CityClient;
import com.panda.springcloud.clients.ClazzClient;
import com.panda.springcloud.clients.GroupClient;
import com.panda.springcloud.clients.TagClient;
import com.panda.springcloud.entity.*;
import com.panda.springcloud.form.StudentForm;
import com.panda.springcloud.mapper.StudentTagMapper;
import com.panda.springcloud.service.StudentService;
import com.panda.springcloud.mapper.StudentMapper;
import com.panda.springcloud.utils.DateUtil;
import com.panda.springcloud.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 乐于吕
 * @description 针对表【t_student】的数据库操作Service实现
 * @createDate 2024-10-23 11:11:33
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {


    @Resource
    private CityClient cityClient;

    @Resource
    private ClazzClient clazzClient;

    @Resource
    private GroupClient groupClient;

    @Resource
    private StudentTagMapper studentTagMapper;

    @Resource
    private TagClient tagClient;

    @Override
    public List<StudentVO> studentList() {

        List<Student> students = baseMapper.selectList(null);

        List<StudentVO> studentVOS = students.stream().map(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);

            // 城市信息
            Integer cityid = student.getCityid();
            City oneCity = cityClient.getCityById(cityid);
            studentVO.setCity(oneCity);

            // 班级信息
            Integer clazzid = student.getClazzid();
            Clazz clazz = clazzClient.getClazzById(clazzid);
            studentVO.setClazz(clazz);

            // 小组信息
            Integer groupid = student.getGroupid();
            Group oneGroup = groupClient.getGroupById(groupid);
            studentVO.setGroup(oneGroup);

            // 标签信息
            Integer studentId = student.getId();
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid, studentId);

            List<Integer> tIds = studentTagMapper.selectList(lambda)
                    .stream().map(studentTag -> studentTag.getTid())
                    .collect(Collectors.toList());

            List<Tag> tags = tIds.stream().map(tid -> {
                Tag tag = tagClient.getTagById(tid);
                return tag;
            }).collect(Collectors.toList());

            studentVO.setTags(tags);

            return studentVO;
        }).collect(Collectors.toList());

        return studentVOS;
    }

    @Override
    public StudentVO addStudent(StudentForm studentForm) {

        // student
        Student student = new Student();
        BeanUtils.copyProperties(studentForm, student);

        // 根据学生的出生日期计算学生的其它相关属性值
        Date bir = student.getBir();
        int age = DateUtil.getAge(bir);
        String attr = DateUtil.getYear(bir);
        String stars = DateUtil.getConstellation(bir);

        student.setAge(age);
        student.setAttr(attr);
        student.setStarts(stars);

        baseMapper.insert(student);
        Integer sid = student.getId();

        List<Integer> tagIds = studentForm.getTagIds();

        //   sid  tid  student_tag
        tagIds.forEach(tid -> {
            StudentTag studentTag = new StudentTag();
            studentTag.setTid(tid);
            studentTag.setSid(sid);
            studentTagMapper.insert(studentTag);
        });


        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student, studentVO);

        // 城市信息
        Integer cityid = student.getCityid();
        City oneCity = cityClient.getCityById(cityid);
        studentVO.setCity(oneCity);

        // 班级信息
        Integer clazzid = student.getClazzid();
        Clazz clazz = clazzClient.getClazzById(clazzid);
        studentVO.setClazz(clazz);

        // 小组信息
        Integer groupid = student.getGroupid();
        Group oneGroup = groupClient.getGroupById(groupid);
        studentVO.setGroup(oneGroup);


        List<Tag> tags = tagIds.stream().map(tid -> {
            Tag tag = tagClient.getTagById(tid);
            return tag;
        }).collect(Collectors.toList());

        studentVO.setTags(tags);

        return studentVO;
    }
}




